using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Security.Cryptography;
using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;
using PlugNT.App.Bbs.DAL;
using PlugNT.App.Bbs.Model;

namespace PlugNT.App.Bbs.DAL
{
    public class Reply
    {

        internal static string currTableName = WebConfig.TablePrefix + "b_reply";











        public ReplyInfo GetReplyInfo(int id)
        {
            ReplyInfo model = null;
            IDataReader dr = GetReader(1, "*", " and id=" + id);
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new ReplyInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.no = dr["no"].ToString();
                    model.sys_model_id = int.Parse(dr["sys_model_id"].ToString());
                    model.channel_id = int.Parse(dr["channel_id"].ToString());
                    model.username = dr["username"].ToString();
                    model.ip = dr["ip"].ToString();
                    model.locked = dr["locked"].ToString() == "1";
                    model.title = dr["title"].ToString();
                    model.content = dr["content"].ToString();
                    model.addtime = DateTime.Parse(dr["addtime"].ToString());
                    model.article_id = int.Parse(dr["article_id"].ToString());
                    model.address = dr["address"].ToString();
                    model.tip = int.Parse(dr["tip"].ToString());
                    model.layer = int.Parse(dr["layer"].ToString());
                }
                dr.Close();
            }
            return model;
        }






        private int GetId(string no)
        {
            object obj = DbHelper.GetSingle("select top 1 [id] from " + currTableName + " where [no]='" + StringHelper.SqlFilter(no)+"'");
            return (obj == null) ? 0 : int.Parse(obj.ToString());
        }








        private int GetArticleId(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 article_id from " + currTableName);
            strSql.Append(" where id=" + id.ToString());
            object obj = DbHelper.GetSingle(strSql.ToString());
            return (obj != null) ? int.Parse(obj.ToString()) : 0;
        }






        public bool Add(ReplyInfo model)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + "(");
            strSql.Append("[no],sys_model_id,channel_id,username,ip,locked,title,content,addtime,article_id,address,tip,layer");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + StringHelper.SqlFilter(model.no) + "',");
            strSql.Append(model.sys_model_id.ToString() + ",");
            strSql.Append(model.channel_id.ToString() + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.username) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.ip) + "',");
            strSql.Append((model.locked ? "1" : "0") + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.title) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.content) + "',");
            strSql.Append(WebConfig.IsAccess ? "Now()," : "getdate(),");
            strSql.Append(model.article_id.ToString() + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.address) + "',");
            strSql.Append(model.tip.ToString() + ",");
            strSql.Append(model.layer.ToString());
            strSql.Append(")");

            

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0)
            {
                Section.UserReply(model.sys_model_id, model.username, true);
                Section.SetLastReply(model.sys_model_id, GetId(model.no), model.article_id, model.title, model.username, DateTime.Now);
                Section.RemoveReplyCount(model.sys_model_id, model.channel_id, true);
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool AddTip(int id)
        {

            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("tip=tip+1");
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }
        public bool UpdateContent(int id, int layer, string content)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("content='" + PlugNT.Safe.StringHelper.SqlFilter(content) + "',layer=" + layer.ToString());
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }
        public bool UpdateTip(int id, int tip)
        {

            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("tip=" + tip.ToString());
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }
        public bool UpdateLocked(int id, bool locked)
        {

            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("locked=" + (locked ? "1" : "0"));
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool Delete(int sysModelId, int channelId, int id,string username, bool isToday)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where id=" + id.ToString());

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0)
            {
                Section.UserReply(sysModelId,username, false);
                Section.RemoveReplyCount(sysModelId, channelId, isToday);
                return true;
            }
            else
            {
                return false;
            }
        }













        private IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }







        public DataTable GetList(int sysModelId, string andWhere)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from  [" + currTableName + "] where sys_model_id= " + sysModelId);
            strSql.Append(andWhere);
            return DbHelper.GetDataTable(strSql.ToString());
        }


        public DataTable GetList(int sysModelId, int pageSize, int currPage, string andWhere, bool isReverse)
        {
            string sql = "";
            if (isReverse)
            {
                sql = Sql.GetRePagingString(pageSize, currPage, "[" + currTableName + "]", "id", " and sys_model_id=" + sysModelId + andWhere);
            }
            else
            {
                sql = Sql.GetPagingString(pageSize, currPage, "[" + currTableName + "]", "id", " and sys_model_id=" + sysModelId + andWhere);
            }
            return DbHelper.GetDataTable(sql);
        }




        public DataTable GetList(int sysModelId, int pageSize, int currPage, string columns, string andWhere, bool isReverse, LeftJoinInfo joininfo)
        {
            string sql = "";
            if (isReverse)
            {
                sql = Sql.GetRePagingString(pageSize, currPage, currTableName, "id", columns, " and sys_model_id=" + sysModelId + andWhere, joininfo);
            }
            else
            {
                sql = Sql.GetPagingString(pageSize, currPage, currTableName, "id", columns, " and sys_model_id=" + sysModelId + andWhere, joininfo);
            }
            return DbHelper.GetDataTable(sql);
        }




        public static int GetCount(int sysModelId, string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where sys_model_id= " + sysModelId + andWhere;
            return (int)DbHelper.GetSingle(sql);
        }




    }
}

